package com.kai.service.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kai.service.article.entity.Article;
import com.kai.service.article.service.IArticleService;
import com.kai.service.data.request.user.UpdateUserColumn;
import com.kai.service.data.request.user.UserColumnRequest;
import com.kai.service.data.response.user.UserColumnResponse;
import com.kai.service.enums.ResultCodeEnum;
import com.kai.service.exception.GenericException;
import com.kai.service.user.entity.UserColumn;
import com.kai.service.user.mapper.UserColumnMapper;
import com.kai.service.user.service.IUserColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户专栏表 服务实现类
 * </p>
 *
 * @author wjk
 * @since 2022-03-20
 */
@Service
public class UserColumnServiceImpl extends ServiceImpl<UserColumnMapper, UserColumn> implements IUserColumnService {

    @Autowired
    private IArticleService articleService;

    /**
     * 新增用户专栏
     *
     * @param userColumnRequest
     * @return
     */
    @Override
    public Boolean createUserColumn(UserColumnRequest userColumnRequest) {
        Long userId = userColumnRequest.getUserId();
        String columnName = userColumnRequest.getColumnName();
        UserColumn one = this.getOne(new LambdaQueryWrapper<UserColumn>().eq(UserColumn::getUserId, userId)
                .eq(UserColumn::getColumnName, columnName));
        if (null != one){
            throw new GenericException(ResultCodeEnum.COLUMN_EXISTED);
        }
        Date nowDate = new Date();
        UserColumn userColumn = new UserColumn();
        userColumn.setUserId(userId).setColumnName(columnName)
                .setCreateDatetime(nowDate).setUpdateDatetime(nowDate);

        return this.save(userColumn);
    }

    /**
     * 查询用户专栏列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserColumnResponse> userColumn(Long userId) {
        List<UserColumn> userColumnList = this.list(new LambdaQueryWrapper<UserColumn>().eq(UserColumn::getUserId, userId)
                .orderByDesc(UserColumn::getCreateDatetime));
        List<UserColumnResponse> userColumnResponseList = new ArrayList<>();
        for (UserColumn userColumn : userColumnList) {
            UserColumnResponse userColumnResponse = new UserColumnResponse();
            BeanUtils.copyProperties(userColumn, userColumnResponse);
            userColumnResponseList.add(userColumnResponse);
        }
        return userColumnResponseList;
    }

    /**
     * 修改用户专栏名称
     *
     * @param columnId
     * @param updateUserColumn
     * @return
     */
    @Override
    public Boolean updateColumn(Integer columnId, UpdateUserColumn updateUserColumn) {
        UserColumn userColumn = this.getById(columnId);
        if (null == userColumn){
            throw new GenericException(ResultCodeEnum.COLUMN_NOT_EXIST);
        }
        return userColumn.setColumnName(updateUserColumn.getColumnName())
                .setUpdateDatetime(new Date()).updateById();
    }

    /**
     * 删除用户专栏
     *
     * @param columnId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean deleteColumn(Integer columnId) {
        articleService.update(new LambdaUpdateWrapper<Article>().eq(Article::getColumnId, columnId).set(Article::getColumnId, 0));
        this.removeById(columnId);

        return Boolean.TRUE;
    }
}
